package cn.edu.nju.newsFeed;

import java.util.Calendar;
import java.util.Locale;

import cn.edu.nju.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.format.DateFormat;

public abstract class FeedItem{
	protected long postUserId;
	protected long postId; // postId may be comment id or news feed id
	protected Bitmap avatar;
	protected String postUserName;
	protected String postContent;
	protected Calendar postTime;
	protected int likeNumber;
	private Activity activity;
	
	protected FeedItem(long postUserId, long postId, Bitmap avatar, String postUserName,
						String postContent, Calendar time, int likeNumber,Activity activity){
		this.postUserId = postUserId;
		this.postId = postId;
		this.avatar = avatar;
		this.postUserName = postUserName;
		this.postContent = postContent;
		this.postTime = time;
		this.likeNumber = likeNumber;
		this.activity = activity;
	}
	
	public Bitmap getAvatar(){
		return avatar;
	}
	
	public String getPostUserName(){
		return postUserName;
	}
	
	public String getPostContent(){
		return postContent;
	}
	
	public String getPostTime(){
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		
		long currentTime = calendar.getTimeInMillis();
		long releaseTime = postTime.getTimeInMillis();
		
		long dif_time = currentTime - releaseTime;
		
		String time = "";
		
		long difSec = 1000;
		long difMin = difSec * 60;
		long difHour = difMin * 60;
		long difDay = difHour * 24;
		
		dif_time -= difHour * 12;
		
		if(dif_time >= difDay * 7){
			time = DateFormat.format("yyyy-MM-dd", releaseTime) + "";
		} else if(dif_time >= difDay) {
			time = dif_time / difDay + activity.getString(R.string.day) + activity.getString(R.string.before);
		} else if(dif_time >= difHour){
			time =  dif_time / difHour + activity.getString(R.string.hour) + activity.getString(R.string.before);
		} else if (dif_time >= difMin) {
			time = dif_time / difMin + activity.getString(R.string.miniute) + activity.getString(R.string.before);
		} else if (dif_time >= difSec) {
			time = dif_time / difSec + activity.getString(R.string.second) + activity.getString(R.string.before);
		} else {
			time = activity.getString(R.string.just_rel);
		}
		
		return time;
		
	}
	
	public int getLikeNumber(){
		return likeNumber;
	}
	
	// derived class should update the server
	public void addOneLike(){
		likeNumber++;
	}
}
